﻿using System;
using System.Xml;

namespace Moonlite.Services.CodeCompletion
{
    public static class JassNativeCodeReader
    {
        private const string Path = "Moonlite.Services.CodeCompletion.Resources.Xml.JassNativeCode.xml";

        public static JassCodeObjectCollection Read()
        {
            JassCodeObjectCollection objectCollection = new JassCodeObjectCollection();
            XmlDocument document = new XmlDocument();
            document.Load(typeof(JassNativeCodeReader).Assembly.GetManifestResourceStream(Path));
            foreach (XmlNode node in document.ChildNodes)
            {
                if (node.Name.ToLower() == "jass")
                {
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        switch (childNode.Name.ToLower())
                        {
                            case "natives":
                                foreach (XmlNode nativeChildNode in childNode.ChildNodes)
                                {
                                    JassCodeObject codeObject = new JassCodeObject();
                                    switch (nativeChildNode.Name.ToLower())
                                    {
                                        case "variable":
                                            codeObject.Type = JassCodeObjectType.Variable;
                                            break;
                                        case "native":
                                            codeObject.Type = JassCodeObjectType.Native;
                                            break;
                                        default:
                                            break;
                                    }

                                    foreach (XmlAttribute attribute in nativeChildNode.Attributes)
                                    {
                                        switch (attribute.Name.ToLower())
                                        {
                                            case "name":
                                                codeObject.Name = attribute.Value;
                                                break;
                                            default:
                                                break;
                                        }
                                    }

                                    codeObject.Code = nativeChildNode.InnerText;
                                    objectCollection.Add(codeObject);
                                }

                                break;
                            case "bjs":
                                foreach (XmlNode bjChildNode in childNode.ChildNodes)
                                {
                                    JassCodeObject codeObject = new JassCodeObject();
                                    switch (bjChildNode.Name.ToLower())
                                    {
                                        case "variable":
                                            codeObject.Type = JassCodeObjectType.Variable;
                                            break;
                                        case "function":
                                            codeObject.Type = JassCodeObjectType.BJ;
                                            break;
                                        default:
                                            break;
                                    }

                                    foreach (XmlAttribute attribute in bjChildNode.Attributes)
                                    {
                                        switch (attribute.Name.ToLower())
                                        {
                                            case "name":
                                                codeObject.Name = attribute.Value;
                                                break;
                                            default:
                                                break;
                                        }
                                    }

                                    codeObject.Code = bjChildNode.InnerText;
                                    objectCollection.Add(codeObject);
                                }

                                break;
                            default:
                                break;
                        }
                    }
                }
            }

            return objectCollection;
        }
    }
}
